package org.nch.videoLauncher.presenter;

import org.nch.videoLauncher.view.IMainWindowView;
import org.nch.videoLauncher.view.NavButtonView;
import org.nch.videoLauncher.domain.Profile;
import org.nch.videoLauncher.repository.ProfileRepository;
import org.nch.videoLauncher.task.ProfileUtil;
import org.nch.videoLauncher.task.ProfileTask;

import java.util.prefs.Preferences;
import java.util.List;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: bill
 * Date: Nov 18, 2008
 * Time: 12:34:55 PM
 * To change this template use File | Settings | File Templates.
 */
public class MainWindowPresenter {

    private IMainWindowView mainWindowView;
    private ProfileTask profileTask;
    private List<Profile> profiles;
    private List<String> profileNames;
    private Profile currentProfile;
    private String searchTerm;

    public MainWindowPresenter(IMainWindowView mwv){
        this.mainWindowView = mwv;
        
    }

    public void initialize(){

        mainWindowView.setTitleText("VideoBrain! (BETA)");
//        mainWindowView.setProfileName(ProfileUtil.getProfile().getName());

        profileTask = new ProfileTask();
        profileNames = new ArrayList<String>();
        currentProfile = ProfileUtil.getProfile();
        setProfileList();

        // load up the inital content panel

        setNavButtonView();
    }

    public void setMainWindowMessageText(String message){
        mainWindowView.setMessageContainerText(message);
    }

    public void setNavButtonView(){

        mainWindowView.setNavButtonView();
        mainWindowView.setMessageContainerText("Welcome to Video Launcher.  Select one of the buttons to get started.");

    }

    public void setAddEditVideoView(){

        mainWindowView.setAddEditVideoView();
        mainWindowView.setMessageContainerText("Select a Video Item from the list, or press add or delete to modify the list.");

    }

    public void setAddEditProfileView(){

        mainWindowView.setAddEditProfileView();
        mainWindowView.setMessageContainerText("Select a profile from the list, or press add to create a new profile.");

    }

    public void setBrowseVideoView(){
        mainWindowView.setBrowseVideoView();

    }

    public void setHistoryView(){
        mainWindowView.setHistoryView();

    }

    public void setSearchView(){
        mainWindowView.setSearchView();

    }

    public void setProfileName(String name){
        mainWindowView.setSelectedProfile(name);
    }

    public void setProfileList(){

        profiles = profileTask.getAllProfiles();
        profileNames.clear();
        
        for (Profile p: profiles){
            profileNames.add(p.getName());
        }
        mainWindowView.setProfileList(profileNames);
        mainWindowView.setSelectedProfile(ProfileUtil.getProfile().getName());
    }

    public void profileSelectionChanged(){

        // find the selected profile by name

        String profileName = mainWindowView.getSelectedProfile();

        for (Profile p: profiles){
            if (p.getName().equals(profileName)){
                ProfileUtil.setActiveProfileId(p.getId());
            }
        }

        // change the active profile to match the selected value

    }

    public void onExit(){

        // save the current profile id and exit
        ProfileUtil.saveActiveProfileId();
        System.exit(0);
    }

    public void quickSearch(){
        searchTerm = mainWindowView.getSearchText();
        mainWindowView.setSearchView(searchTerm);
    }

}
